package org.example2;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

// 只能加载普通jar包, 不能加载嵌套的jar包
public class UseClassInNestedJar {
    public static void main(String[] args)throws Exception {
        System.out.println("---------------------------");
        String jarPath = System.getProperty("user.dir") + "/jarFileRear/jarFileDir/nestedJar.jar";

        System.out.println(jarPath);

        ClassLoader classLoader = UseClassInNestedJar.class.getClassLoader();
        URL url = new URL("file:/"+jarPath);

        System.out.println("file:/"+jarPath);
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url}, classLoader);

        System.out.println("111");
        //外层jar包中的class,可以被正确加载
//        Class<?> nestedClass = urlClassLoader.loadClass("org.example.App");

        //嵌套jar包中的class不能被正确加载
        Class<?> nestedClass = urlClassLoader.loadClass("org.example.HelloWorld");
        Object nestedObject = nestedClass.newInstance();
        System.out.println("222");
        Method main = nestedClass.getMethod("main", String[].class) ;
        main.invoke(null, (Object)new String[0]);

    }
}
